#ifndef __DBUS_HANDLER_H__
#define __DBUS_HANDLER_H__
/* Includes ------------------------------------------------------------------*/
#include <dbus/dbus.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <mutex>

#include "Mutex.hxx"
#include "Template.hxx"
#include "types.hxx"
#include "Thread.hxx"
/* define --------------------------------------------------------------------*/
/* typedef -------------------------------------------------------------------*/
typedef struct
{
    const char *pstrObjectName;
    const char *pstrInterface;
    const char *pstrMethod;
    const std::string (*pfnInvoke)(const std::string &strValue); // 指向服务响应处理函数，并返回响应字符串
} DBUS_SERVICES;                                                 // 对外提供的接口服务
/* variables -----------------------------------------------------------------*/
/* Private function(only *.c)  -----------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/*
 * EOF
 */

// https://www.cnblogs.com/wangshenyang/p/5781103.html

class CDbusHandler : public CThread
{
    PATTERN_SINGLETON_DECLARE(CDbusHandler);

public:
    /// @brief 初始化
    bool Init(const char *pstDbusName);
    /// @brief 去始化
    bool CleanUp();
    /// @brief 方法发送（只发送）
    bool MethodSend(const char *pstrDbusDstName, const char *pstrObjectName, const char *pstrInterface,
                    const char *pstrMethod, const std::string &strValue);
    /// @brief 方法发送（发送并接收回应）
    bool MethodSendGetReply(const char *pstrDbusDstName, const char *pstrObjectName, const char *pstrInterface,
                            const char *pstrMethod, const std::string &strValue, std::string &strReply);
    /// @brief 信号发送
    bool SignalSend(const char *pstrObjectName, const char *pstrInterface, const char *pstrMethod,
                    const char *pstrValue);

    /// @brief 注册回调函数
    bool RegisteredServices(const char *pstrObjectName, const char *pstrtInterface, const char *pstrMethod,
                            const std::string (*pfnRegistrationCallback)(const std::string &pstrValue),
                            bool isSingnal = false);
    /// @brief 反注册
    bool UnRegisteredServices(const char *pstrObjectName, const char *pstrtInterface, const char *pstrMethod);

private:
    CDbusHandler();
    CDbusHandler(const CDbusHandler &) = delete;            // 移动构造函数
    CDbusHandler &operator=(const CDbusHandler &) = delete; // 移动赋值运算符
    virtual ~CDbusHandler();

    /// @brief 信号监听注册
    void SignalListen(const char *pstrInterface, const char *pstrMethod);
    /// @brief 检查是否有消息到来
    bool QueryMessage(int s32TimeoutMs = 20);
    /// @brief 根据注册表接收对应消息并回调到注册函数
    bool HandleMessage();
    /// @brief 线程体
    virtual void ThreadProc();

private:
    DBusConnection *m_dbusConn;
    CMutex m_Mutex;
    std::mutex m_Lock;
    std::vector<DBUS_SERVICES> m_VcDbusServices;
};

// dbus_connection_set_watch_functions()
#define g_DbusHandler (*CDbusHandler::instance())

#endif /* __DBUS_HANDLER_H__ */
